<template>
	<view class="content">
		<!-- <u-navbar title="会员团购券" :placeholder='true' height="88rpx" leftIconColor="#fff" -->
		<u-navbar title="会员卡券" :placeholder='true' height="88rpx" leftIconColor="#fff"
			titleStyle='color:#fff;font-size:32rpx' left-icon-size="32rpx" bg-color="#FF594D" :autoBack="true">
		</u-navbar>
		<view class="filtrate">
			<search-button-input @getValue="getSearch"></search-button-input>
			<view class="check_all">
				<u-checkbox-group activeColor="#FF594D" v-model="checkAll" placement="column" @change="allChange">
					<view class="all">
						<u-checkbox size="30rpx" shape='circle' name="name"></u-checkbox>
						<view class="right">
							全选
						</view>
					</view>
				</u-checkbox-group>
				<scroll-view scroll-x="true" style="width: 600rpx; ">
					<view class="condition_list">
						<template v-for="(item,index) in tagData">
							<view style="white-space: nowrap; padding: 0 25rpx;"
								:class="{condition_item:true,active:conditionIndex==index}" @click="conditionIndex=index" :key="index">
								{{item.labelName}}
							</view>
						</template>
					</view>
				</scroll-view>
				<!-- <view class="plus_btn" style="margin-left:140rpx;" @click="addMem">
				<u-icon name="plus" size="32rpx"></u-icon>
			</view> -->
			</view>
		</view>
		<scroll-view scroll-y="true">
			<view class="clientele">
				<u-checkbox-group activeColor="#FF594D" v-model="checkboxValue1" placement="column" @change="checkChange">
					<template v-for="(item,index) in memberData">
						<view class="clientele_item" :key="index">
							<view class="item_left">
								<u-checkbox size="30rpx" shape='circle' :name="item.id+''"></u-checkbox>
								<view class="client_info">
									{{item.memberybDTO.customerInfo.name}} &nbsp;{{item.memberybDTO.customerInfo.phone}}
								</view>
							</view>
							<view class="item_right">
								<image @click="navigateTo('/pages_spell/pages/member-change/member-change?id='+item.id)"
									src="/static/images/factionalism/编辑@2x.png" class="edit" mode=""></image>
								<view class="arrow" @click="navigateTo('/pages_spell/pages/member-detail/member-detail?id='+item.id)">
									<image src="/static/images/mine/图标@2x.png" mode="" calss="arrow"></image>
								</view>
							</view>
						</view>
					</template>
				</u-checkbox-group>
			</view>
		</scroll-view>

		<view class="bottom">
			<view class="left" @click="getCouponList">
				<!-- 发送团购券 -->
				发送卡券
			</view>
			<u-line direction='col' color="#fff"></u-line>
			<view class="right" @click="navigateTo('/pages_spell/pages/coupon-record/coupon-record')">
				<!-- 团购券记录 -->
				卡券记录
			</view>
		</view>
		<u-popup :show="delModel_show" round="10" mode="center">
			<view class="delModel">
				<view class="delText">
					请选择卡券
				</view>
				<view class="money">
					<view class="client_num">
						所选客户数量：{{checkboxValue1.length}}
					</view>
					<u-radio-group activeColor="#FF594D" labelSize="26rpx" v-model="radiovalue1" placement="column"
						@change="groupChange">
						<u-radio :customStyle="{marginBottom: '16rpx'}" :labelColor='radiovalue1==item.id?"#777777":"#D4D4D4"'
							v-for="(item, index) in couponData" :key="index" :label="item.name" :name="item.id" @change="radioChange">
						</u-radio>
					</u-radio-group>
				</view>
				<u-line></u-line>
				<view class="operation">
					<view @click="delModel_show = false" class="btn operation_left">
						取消
					</view>
					<u-line direction='col'></u-line>
					<view @click="sendCop" class="btn operation_right">
						确定
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		memberList,
		addMember,
		tagList,
		couponList,
		empSendCop
	} from '../../../api/employee.js'
	export default {
		data() {
			return {
				titleStyle: {
					color: '#fff',
				},
				memberData: [],
				memberList: [],
				checkboxValue1: [],
				checkAll: [],
				loginCode: 0,
				tagData: [],
				conditionIndex: 0,
				phone: '',
				couponData: [],
				delModel_show: false,
				radiovalue1: '',
			}
		},
		watch: {
			conditionIndex(newValue, oldValue) {
				this.checkAll = []
				this.checkboxValue1 = []
				console.log(newValue, oldValue);
				if (newValue == 0) {
					// this.memberData = this.memberList
					this.memberData = this.memberList.filter((item, index) => {
						if (this.phone.length >= 11 && item.phone == this.phone) {
							console.log('shoujihaoxiangdneg ');
							return item
						} else if (this.phone.length == 0) {
							return item
						}
						// console.log(item);
					})
					return
				}
				// console.log(this.tagData[newValue - 1]);
				this.memberData = this.memberList.filter((item, index) => {
					if (item.departmentId == this.tagData[newValue].id) {
						if (this.phone.length >= 11 && item.phone == this.phone) {
							console.log('shoujihaoxiangdneg ');
							return item
						} else if (this.phone.length == 0) {
							return item
						}
						// console.log(item);
					}
				})
			},
			memberData(newValue) {
				if (newValue.length == 0) {
					uni.showToast({
						title: '无此条件会员',
						icon: 'error'
					})
				}
			}
		},
		created() {
			this.getMemberList()
			this.getTagList()
		},
		methods: {
			async sendCop() {

				const checkUser = []
				this.checkboxValue1.forEach(id => {
					// let member = null
					try {
						this.memberData.forEach(item => {
							if (id == item.id) {
								throw new Error(JSON.stringify(item))
							}
						})
					} catch (e) {
						// console.log(JSON.parse(e.message));
						const member = JSON.parse(e.message)
						checkUser.push(member.userId)
					}
				})
				const res = await empSendCop({
					userId: checkUser,
					couponId: this.radiovalue1,
					senderName: JSON.parse(uni.getStorageSync('employeeInfo')).userName
				})
				console.log(res);
				this.delModel_show = false
				if (res.code != 200) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					return
				}
				uni.showToast({
					title: '发送成功'
				})
			},
			async getCouponList() {
				if (this.checkboxValue1.length == 0) {
					uni.showToast({
						title: '请选择一个客户',
						icon: 'error'
					})
					return
				}
				this.delModel_show = true
				const res = await couponList()
				console.log(res);
				if (res.code != 200) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					return
				}
				this.couponData = res.rows
			},
			async getTagList() {
				const res = await tagList()
				console.log(res);
				if (res.code != 200) {
					// uni.showToast({
					// 	title: '获取失败',
					// 	icon: 'error'
					// })
					this.tagData.unshift({
						labelName: '全部'
					})
					return
				}
				this.tagData = res.rows
				this.tagData.unshift({
					labelName: '全部'
				})
			},
			changeCondition() {

			},
			getSearch(value) {
				// console.log(value);
				this.phone = value
				this.checkAll = []
				this.checkboxValue1 = []
				if (value.length < 11 || !Boolean(value - 0)) {
					if (value.length === 0) {
						if (this.conditionIndex == 0) {
							this.memberData = this.memberList
							return
						}
						this.memberData = this.memberData.filter((item, index) => {
							if (item.departmentId == this.tagData[this.conditionIndex].id) {
								return item
							}
						})
						return
					}
					uni.showToast({
						title: '输入正确手机号',
						icon: 'error'
					})
					return
				}
				this.memberData = this.memberData.filter(item => {
					if (item.phone == this.phone) {
						console.log(item);
						return item
					}
				})

			},
			toTag() {
				const checkUser = []
				this.checkboxValue1.forEach(id => {
					// let member = null
					try {
						this.memberData.forEach(item => {
							if (id == item.id) {
								// console.log(item);
								// member = item
								throw new Error(JSON.stringify(item))
							}
						})
					} catch (e) {
						console.log(JSON.parse(e.message));
						// if (e.message) {
						// return e.message
						// }
						const member = JSON.parse(e.message)
						checkUser.push({
							id: member.id,
							customerUid: member.customerUid
						})
					}
				})
				// console.log(checkUser);
				if (checkUser[0]) {
					uni.redirectTo({
						url: '/pages_spell/pages/mine-tags/mine-tags?ids=' + JSON.stringify(checkUser)
					})
					return
				}
				uni.navigateTo({
					url: '/pages_spell/pages/mine-tags/mine-tags'
				})
			},
			async getMemberList() {
				const res = await memberList()
				console.log(res);
				if (res.code != 200) {
					uni.showToast({
						title: '登录过期',
						icon: 'error'
					})
					return
				}
				this.memberData = res.rows
				this.memberList = res.rows
			},
			async addMem() {
				const res = await addMember()
				console.log(res);
			},
			allChange(n) {
				console.log(this.checkAll);
				if (n[0] == 'name') {
					this.memberData.forEach((item, index) => {
						this.checkboxValue1[index] = item.id
					})
				} else {
					this.checkboxValue1 = []
				}
			},
			checkChange(n) {
				console.log(n);

				if (n.length == this.memberData.length) {
					this.checkAll[0] = 'name'
				} else {
					this.checkAll[0] = ''
				}
			},
			navigateTo(url) {
				uni.navigateTo({
					url
				})
			},
			groupChange(n) {
				// console.log('groupChange', n);

			},
			radioChange(n) {
				// console.log('radioChange', n);
			}
		}
	}
</script>


<style lang="scss">
	.content {
		background-color: #f3f3f3;
		min-height: 100vh;

		.u-safe-area-inset-bottom {
			padding-bottom: 0;
		}

		.filtrate {
			background-color: #fff;

			.check_all {
				padding: 0 20rpx 18rpx;
				font-size: 20rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #777777;
				display: flex;
				align-items: center;
				// justify-content: space-between;

				.all {
					display: flex;
					align-items: center;

					.right {
						padding-left: 10rpx;
						padding-right: 30rpx;
					}
				}

				.condition_list {
					text-align: center;
					display: flex;
					align-items: center;

					.condition_item {
						background-color: #F3F3F3;
						// width: 124rpx;
						height: 56rpx;
						border-radius: 99rpx;
						line-height: 56rpx;
						margin-right: 20rpx;

						&.active {
							background-color: #FF594D;
							color: #fff;
						}
					}
				}
			}
		}


		.clientele {
			padding: 20rpx 20rpx 0;
			padding-bottom: 200rpx;

			.clientele_item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 56rpx;
				margin-bottom: 20rpx;

				.item_left {
					display: flex;
					align-items: center;

					.client_info {
						margin-left: 10rpx;
						font-size: 26rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						color: #777777;
					}
				}

				.item_right {
					display: flex;
					align-items: center;

					.edit {
						width: 32rpx;
						height: 32rpx;
						margin-right: 22rpx;
					}

					.arrow {
						width: 40rpx;
						height: 100%;
						display: flex;
						align-items: center;
						justify-content: center;

						image {
							width: 17rpx;
							height: 32rpx;
						}
					}
				}
			}
		}

		.bottom {
			width: 752rpx;
			height: 120rpx;
			background-color: #FFFFFF;
			position: fixed;
			left: 0;
			bottom: 0;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.left,
			.right {
				flex: 1;
				height: 120rpx;
				background: #FF594D;
				text-align: center;
				line-height: 120rpx;
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
			}

			.left {}

			.right {}
		}

		.delModel {
			width: 690rpx;
			// height: 387rpx;
			box-sizing: border-box;
			// // #ifdef MP
			// height: 334rpx;
			// z-index: 100;
			// position: relative;
			// // #endif
			// padding: 24rpx 20rpx;

			.delTitle {
				font-size: 26rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #333332;
				display: flex;
				justify-content: space-between;


				image {
					width: 32rpx;
					height: 32rpx;
				}
			}

			.delText {
				// height: 250rpx;
				text-align: center;
				padding: 30rpx 40rpx;
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #777777;
				color: #333332;
			}

			.adjustment_num {
				display: flex;
				align-items: center;
				width: 216rpx;

				button {
					background-color: transparent;
					padding: 0;
					border: 0;
					color: #777777;
					font-size: 44rpx;

					&::after {
						border: 0;
					}
				}

				input {
					background-color: #FF594D;
					border-radius: 55rpx;
					width: 116rpx;
					height: 51rpx;
					color: #fff;
					text-align: center;
					padding: 0;
					margin: 0 10rpx;
				}
			}

			.money {
				padding: 0 120rpx 20rpx;

				.client_num {
					font-size: 26rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					color: #777777;
					text-align: center;
					padding-bottom: 20rpx;
				}
			}

			.adjustment_num {
				display: flex;
				align-items: center;
				width: 216rpx;
				justify-content: center;
				width: 100%;
				padding: 0 100rpx;
				box-sizing: border-box;

				button {
					background-color: transparent;
					padding: 0;
					border: 0;
					color: #777777;
					font-size: 44rpx;

					&::after {
						border: 0;
					}
				}

				input {
					background-color: #FF594D;
					border-radius: 55rpx;
					width: 116rpx;
					height: 51rpx;
					color: #fff;
					text-align: center;
					padding: 0;
					margin: 0 10rpx;
				}
			}

			.operation {
				display: flex;
				justify-content: center;
				height: 120rpx;


				.btn {
					width: 345rpx;
					height: 100%;
					line-height: 56rpx;
					font-size: 32rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					color: #FF594D;
					text-align: center;
					line-height: 110rpx;
					box-sizing: border-box;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
				}

				.operation_left {
					color: #777777;
				}
			}
		}

	}
</style>